/*
 * @Description: 
 * @FilePath: /STM32F103/USER/bsp/iic.h
 * @Author: Wei Zhou
 * @Github: https://github.com/zromyk
 * @Date: 2021-04-28 15:25:19
 * @LastEditors: Wei Zhou
 * @LastEditTime: 2021-05-12 23:54:32
 * @Copyright: Copyright ? 2017 muyiro. All rights reserved.
 */

#ifndef IIC_INIT_H
#define IIC_INIT_H

#include "my_include.h"
#include "gpio.h"

#define IIC_SCL_PORT        GPIOB
#define IIC_SCL_PIN         GPIO_Pin_3

#define IIC_SDA_PORT        GPIOB
#define IIC_SDA_PIN         GPIO_Pin_4

#define SDA_OUT()           do {                                                    \
                                GPIO_InitTypeDef GPIO_InitStructure;                \
                                GPIO_InitStructure.GPIO_Pin = IIC_SDA_PIN;          \
                                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    \
                                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   \
                                GPIO_Init(IIC_SDA_PORT, &GPIO_InitStructure);       \
                            } while(0)
#define SDA_IN()            do {                                                    \
                                GPIO_InitTypeDef GPIO_InitStructure;                \
                                GPIO_InitStructure.GPIO_Pin = IIC_SDA_PIN;          \
                                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;       \
                                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   \
                                GPIO_Init(IIC_SDA_PORT, &GPIO_InitStructure);       \
                            } while(0)

#define IIC_SCL             PBout(3) /* SCL */
#define IIC_SDA             PBout(4) /* SDA */
#define READ_SDA            PBin(4)  /* 输入SDA */

void IIC_init(void);

/**
 * @brief  IIC 操作
 * @param  dev:     器件地址
 * @param  reg:     寄存器地址
 * @param  data:    数据
 * @param  *data:   数据的起始地址
 * @param  len:     数据长度
 * @retval ack
 */
/* 向I2C设备寄存器写入一个字节数据 */
uint8_t IIC_Write_Reg(uint8_t dev, uint8_t reg, uint8_t data);

/* 向I2C设备寄存器读取一个字节数据 */
uint8_t IIC_Read_Reg(uint8_t dev, uint8_t reg, uint8_t *data);

/* 向I2C设备寄存器读取一个字节数据 */
uint8_t IIC_Read_Word(uint8_t dev, uint8_t reg, uint16_t *data);

/* 向I2C设备寄存器读取多个字节数据 */
uint8_t IIC_Read_Regs(uint8_t dev, uint8_t reg, uint8_t *data, uint8_t len);

#endif /* IIC_INIT_H */
